package com.qf.controller;

import com.qf.result.Result;
import com.qf.service.UserSevice;
import com.qf.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 你的名字
 * @Date: 2021/07/05/19:33
 * @Description:
 */
@Controller
@RequestMapping("/userController")
public class UserCotroller {
    @Autowired
    private UserSevice userSevice;
    @PostMapping("/addUser")
    @ResponseBody
    public Result addUser(User user){
        boolean b = userSevice.addUser(user);
        return   Result.write(b);
    }
    @GetMapping("/{page}.html")
    public String toPage(@PathVariable String page) {
        // 此时page就是addUser，userList,xxxx
        return "user/" + page; // user/addUser,user/userList
    }
    @RequestMapping(value = "/delUser/{id}")
    @ResponseBody
    public Result delUser(@PathVariable(value ="id")Integer id){
        System.out.println(id);
        boolean b = userSevice.delUser(id);
        return Result.write(b);
    }
    @RequestMapping("/getUserList")
    public String queryUserList(User user,ModelMap modelMap){
    List<User> userList = userSevice.queryUserList(user);
    modelMap.put("userList",userList);
    modelMap.put("user",user);
    return "user/userList";
}
    @RequestMapping(value = "/getUserById/{id}")
    public String getUserById(@PathVariable(value = "id") Integer id,ModelMap modelMap){
    User user = userSevice.getUserById(id);
    modelMap.put("user",user);
    return "user/updateUser";
}
   @RequestMapping("/updateUser")
   @ResponseBody
    public Result updateUser(User user){
    boolean b = userSevice.updatUser(user);
    return Result.write(b);
}
}
